package me.leiusual.sharing.ui.home.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.view_holder_home_floor_grid.*
import me.leiusual.sharing.api.model.storeindex.ProductLink
import me.leiusual.sharing.util.extension.setImageUrl

/**
 * Create by leiusual on 2018/7/24
 */
class HomeFloorGridViewHolder(
    override val containerView: View,
    recycledViewPool: RecyclerView.RecycledViewPool
) : RecyclerView.ViewHolder(containerView), LayoutContainer {

  init {
    recycler_view.setRecycledViewPool(recycledViewPool)
    recycler_view.adapter = HomeLinkAdapter()
  }

  fun bind(data: HomeHomeAdapter.Item) {
    text_view_title.text = data.title

    val productLink = data.dataSet.firstOrNull {
      it.displayType == ProductLink.BIG_IMAGE
    }

    if (productLink == null) {
      image_view_ad.visibility = View.GONE
    } else {
      image_view_ad.visibility = View.VISIBLE
      image_view_ad.setImageUrl(productLink.image)

      // 显示了大图，下面的就不显示了
      val arrayList = data.dataSet as ArrayList
      arrayList.remove(productLink)
    }

    val adapter = recycler_view.adapter as HomeLinkAdapter
    adapter.submitList(data.dataSet)
  }
}